import 'package:easy_chat_app/commons/GlobalStatus.dart';
import 'package:easy_chat_app/commons/model/AppUserDetail.dart';
import 'package:easy_chat_app/commons/utils/HttpUtils.dart';
import 'package:easy_chat_app/commons/utils/RouteUtils.dart';
import 'package:easy_chat_app/mobile/constants/MobileAppGlobalConstants.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:logger/logger.dart';
import 'package:provider/provider.dart';

class MobileNicknameSettingPageDataChangeNotifier extends ChangeNotifier {
  String _newNickName = "";

  String get newNickName => _newNickName;

  set newNickName(String value) {
    _newNickName = value;
    notifyListeners();
  }

  Future<void> reload() async {
    notifyListeners();
  }
}

class MobileNicknameSettingPage extends StatelessWidget {
  final Logger logger = Logger();

  MobileNicknameSettingPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Consumer<AppUserDetailChangeNotifier>(
        builder: (context, appUserDetailChangeNotifier, child) {
      AppUserDetailChangeNotifier appUserDetailChangeNotifier =
          context.watch<AppUserDetailChangeNotifier>();
      AppUserDetail appUserDetail =
          appUserDetailChangeNotifier.getAppUserDetail();
      TextEditingController textEditingController = TextEditingController();
      textEditingController.text = appUserDetail.nickname ?? "";

      return MultiProvider(
        providers: [
          ChangeNotifierProvider(create: (context) {
            return MobileNicknameSettingPageDataChangeNotifier();
          })
        ],
        builder: (context, child) {
          MobileNicknameSettingPageDataChangeNotifier
              mobileNicknameSettingPageDataChangeNotifier =
              context.watch<MobileNicknameSettingPageDataChangeNotifier>();

          bool enableChangeNickName = true;
          Color completeBtnBackgroundColor =
              const Color.fromRGBO(8, 193, 94, 1);
          Color completeBtnTextColor = Colors.white;
          if (textEditingController.text == appUserDetail.nickname ||
              textEditingController.text.isEmpty) {
            completeBtnBackgroundColor = const Color.fromRGBO(234, 234, 234, 1);
            completeBtnTextColor = const Color.fromRGBO(183, 183, 183, 1);
            enableChangeNickName = false;
          }
          return Scaffold(
            appBar: AppBar(
              title: const Text("设置昵称"),
              centerTitle: true,
              surfaceTintColor: MobileAppGlobalConstants.appBarSurfaceTintColor,
              backgroundColor: MobileAppGlobalConstants.appBarBackgroundColor,
              actions: [
                GestureDetector(
                  child: Container(
                    alignment: Alignment.center,
                    height: 30,
                    margin: const EdgeInsets.only(right: 10),
                    padding: const EdgeInsets.only(left: 10, right: 10),
                    decoration: BoxDecoration(
                        color: completeBtnBackgroundColor,
                        borderRadius: BorderRadius.circular(4)),
                    child: Text(
                      "完成",
                      style:
                          TextStyle(color: completeBtnTextColor, fontSize: 14),
                    ),
                  ),
                  onTap: () async {
                    String newNickName = textEditingController.text;
                    if (enableChangeNickName) {
                      logger.i("修改用户昵称:$newNickName");
                      var result =
                          await HttpUtils.changeUserNickName(newNickName);
                      if (result.code == 0) {
                        await appUserDetailChangeNotifier.reload();
                        RouteUtils.pop();
                      } else {
                        logger.e("result:$result");
                        await Fluttertoast.showToast(
                            msg: result.message ?? "系统错误",
                            toastLength: Toast.LENGTH_SHORT);
                      }
                    } else {
                      logger.i("昵称未发生变化:$newNickName");
                    }
                  },
                ),
              ],
            ),
            body: Container(
              color: const Color.fromRGBO(246, 246, 246, 1),
              child: CupertinoApp(
                debugShowCheckedModeBanner: false,
                home: Container(
                  color: const Color.fromRGBO(246, 246, 246, 1),
                  child: Container(
                    alignment: Alignment.topCenter,
                    margin: const EdgeInsets.only(
                        left: 10, right: 10, top: 10, bottom: 30),
                    child: Container(
                      color: Colors.white,
                      child: CupertinoTextField(
                        controller: textEditingController,
                        decoration: const BoxDecoration(border: null),
                        onChanged: (val) {
                          mobileNicknameSettingPageDataChangeNotifier.reload();
                        },
                      ),
                    ),
                  ),
                ),
              ),
            ),
          );
        },
      );
    });
  }
}
